// API - 系统角色

import { http } from "@/utils/http";
import { errorReject } from "@/utils/truncate";
import { message } from "@/utils/message";
import type { SysMenuInfo } from "./menu";

interface MenusResult extends HttpResponse {
  data?: SysMenuInfo[];
}

interface ListResult extends HttpResponse {
  data?: {
    /** 列表数据 */
    list: SysRoleInfo[];
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
}

interface DetailResult extends HttpResponse {
  data?: SysRoleInfo;
}

export type SysRoleInfo = {
  id: Id;
  roleName: string;
  code: string;
  permissions: string[];
  remark: string;
  sort: string | number;
  status: boolean;
  createTime: string;
  updateTime: string;
};

/**
 * 获取菜单列表
 *
 * @param params 请求参数
 * @returns
 */
export const getSysRoleMenusList = async (params?: object) => {
  return errorReject(
    http.request<MenusResult>("get", "/system/roles/menus", {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取分页列表
 *
 * @param data 请求参数
 * @returns
 */
export const getSysRolePageList = async (data?: object) => {
  return errorReject(
    http.request<ListResult>("post", "/system/roles/page", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取信息
 *
 * @param id 请求ID
 * @param params 请求参数
 * @returns
 */
export const getSysRoleDetail = (id: Id, params?: object) => {
  return errorReject(
    http.request<DetailResult>("get", "/system/roles/" + id, {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 创建信息
 *
 * @param data 请求参数
 * @returns
 */
export const addSysRoleDetail = (data?: object) => {
  return errorReject(
    http.request<DetailResult>("post", "/system/roles", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysRoleDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/roles/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息（菜单权限）
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysRolePermissions = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/roles/" + id + "/permissions", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息（状态）
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysRoleStatus = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/roles/" + id + "/status", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const delSysRoleDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/roles/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 批量删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const batchDelSysRoleDetail = (data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/roles", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};
